# -*- coding: utf-8 -*-
"""
Created on Sat Feb  1 17:53:14 2020

@author: haodong
"""

import numpy as np
import statsmodels.api as sm
import pandas as pd
from sklearn import metrics
import matplotlib.pyplot as plt

df=pd.read_csv('sampling_survey.csv')

#7.3节代码

features=["x1","x2"]
labels = ["y"]
y=df[labels]
X = sm.add_constant(df[features])

#方法一
model = sm.Logit(y, X)
result = model.fit()
result.summary()

#方法二
formula='y~x1+x2'
model2 = sm.Logit.from_formula(formula, data=df)
result2 = model2.fit()
result2.summary()



#以下7.5节代码（第一部分）

#计算概率
y_prob = result.predict(X)
y_prob=pd.DataFrame(y_prob)
y_prob.columns=['y_prob']

alpha=0.6 #设定阈值

#预测因变量归类
y_pred=y_prob.apply(lambda x: 1 if x[0] > alpha else 0, axis=1)
y_pred=pd.DataFrame(y_pred)
y_pred.columns=['y_pred']

#计算混淆矩阵
confusion=metrics.confusion_matrix(y,y_pred)
print(metrics.classification_report(y,y_pred))

#混淆矩阵画图
plt.rcParams["font.sans-serif"]=["SimHei"]
plt.matshow(confusion)
plt.title('混淆矩阵')
plt.colorbar()
plt.ylabel('预测')
plt.xlabel('实际')
plt.show()

# ROC与AUC 计算
fpr, tpr, threshold = metrics.roc_curve(y, y_prob)

ROC_data=np.c_[fpr,tpr,threshold]
ROC_data=pd.DataFrame(ROC_data)
ROC_data.columns=['fpr','tpr','threshold']

auc = metrics.auc(fpr, tpr)



#画ROC曲线图
plt.rcParams["font.sans-serif"]=["SimHei"]
# 创建一个图形框
fig = plt.figure(figsize=(6, 6), dpi=80)
# 在图形框里只画一幅图
ax = fig.add_subplot(1, 1, 1)

# 如使用Python2，str需要decode
ax.set_title("%s" % "ROC曲线")

ax.set_xlabel("False positive rate")
ax.set_ylabel("True positive rate")
ax.plot([0, 1], [0, 1], "r--")
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])

ax.plot(fpr, tpr, "k", label="%s; %s = %0.2f" % ("ROC曲线",
        "曲线下面积（AUC）", auc))
ax.fill_between(fpr, tpr, color="grey", alpha=0.6)
legend = plt.legend(shadow=True)
plt.show()

#sklearn回归(第一次)
trainSet, testSet = train_test_split(df, test_size=0.2, random_state=2105)
model = LogisticRegression()
model.fit(trainSet[features], trainSet[labels])
model.coef_
model.intercept_



#sklearn回归(第二次，调参数)
trainSet, testSet = train_test_split(df, test_size=0.05, random_state=2105)
model = LogisticRegression(C=500)
model.fit(trainSet[features], trainSet[labels])
model.coef_
model.intercept_



#交叉验证
from sklearn.model_selection import train_test_split, cross_val_score
precisions=cross_val_score(model,trainSet[features],trainSet[labels],cv=5,scoring='precision')
print(np.mean(precisions))
recalls=cross_val_score(model,trainSet[features],trainSet[labels],cv=5,scoring='recall')
print(np.mean(recalls))






